home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / Onboard / Exceptions.py < prev    next >
Text File  |  2009-10-01  |  2KB  |  62 lines

  1. from traceback import format_tb
  2. import sys
  3.  
  4. class ChainableError(Exception):
  5.     """
  6.     Base class for Onboard errors
  7.     
  8.     We want Python to print the stacktrace of the first exception in the chain
  9.     so we store the last stacktrace if the previous exception in the chain
  10.     has not.
  11.     """
  12.  
  13.     _last_exception = None
  14.  
  15.     def __init__(self, message, chained_exception = None):
  16.         self._message = message
  17.         self.chained_exception = chained_exception
  18.  
  19.         if chained_exception:
  20.             if not (isinstance(chained_exception, ChainableError) \
  21.                     and chained_exception.traceback):
  22.  
  23.                 # Store last traceback
  24.                 self._last_exception = sys.exc_info()
  25.  
  26.  
  27.     def _get_traceback(self):
  28.         if self._last_exception:
  29.             return self._last_exception[2]
  30.         elif self.chained_exception \
  31.                 and isinstance(self.chained_exception, ChainableError):
  32.             return self.chained_exception.traceback
  33.         else:
  34.             return None
  35.  
  36.     traceback = property(_get_traceback)
  37.  
  38.     def __str__(self):
  39.         message = self._message + "\n"
  40.         if self.chained_exception:
  41.             message += "%s: %s" % (type(self.chained_exception).__name__,
  42.                 str(self.chained_exception))
  43.         return message
  44.  
  45. class SVGSyntaxError(ChainableError):
  46.     """Error raised when Onboard can't comprehend SVG layout file."""
  47.     pass
  48.  
  49. class LayoutFileError(ChainableError):
  50.     """Error raised when Onboard can't comprehend layout definition file."""
  51.     pass
  52.  
  53. def chain_handler(type, value, traceback):
  54.     """
  55.     Wrap the default handler so that we can get the traceback from chained
  56.     exceptions.
  57.     """
  58.     if isinstance(value, ChainableError) and value.traceback:
  59.         traceback = value.traceback
  60.  
  61.     sys.__excepthook__(type, value, traceback)
  62.